package com.hgk.common.common.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hgk.common.common.ui.view.BallTitleView;

import width.hgk.wanwan.R;


/**
 * Created by sam on 15/8/6.
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener{

    protected FragmentActivity mContext;
    protected FragmentManager mFragmentManager;
    protected BallTitleView mTitleView;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = getActivity();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentManager=getChildFragmentManager();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getBodyView() != 0) {
            return inflater.inflate(getBodyView(), mTitleView, false);
        }
        return null;
    }

    abstract int getBodyView();
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.setBackgroundColor(getResources().getColor(R.color.white));
    }

    public void replace(int resid, Fragment fragment) {
        mFragmentManager.beginTransaction().replace(resid, fragment).commitAllowingStateLoss();
    }

    /**
     * @param resid
     * @param fragment
     */
    public void add(int resid, Fragment fragment) {
        if(fragment!=null) {
            mFragmentManager.beginTransaction().add(resid, fragment).commitAllowingStateLoss();
        }
    }

    /**
     * 显示fragment
     *
     * @param fragment
     */
    public void show(Fragment fragment) {
        if(fragment!=null) {
            mFragmentManager.beginTransaction().show(fragment).commitAllowingStateLoss();
        }
    }

    /**
     * 隐藏fragment
     *
     * @param fragment
     */
    public void hide(Fragment fragment) {
        if(fragment!=null) {
            mFragmentManager.beginTransaction().hide(fragment).commitAllowingStateLoss();
        }
    }

    @Override
    public void onClick(View v) {

    }
    @Override
    public void onResume() {
    	super.onResume();
//    	StatService.onResume(this);
    }
    @Override
    public void onPause() {
    	super.onPause();
//    	StatService.onPause(this);
    }
}
